<div class="alert">
<?php
require_once('includes/user_manage.php');

$user->isAdmin or exit("Área reservada a administradores");

if (isset($_POST["item_id"])) {
	$my_item=new admin_item();
	$my_item->id=$_POST["item_id"];
	if ($my_item->id>0) {
		$uploaded=$my_item->upload_picture($_POST["imageType"]);
		if ($uploaded) {
			$my_item->db_load();
			$serItem=json_encode($my_item);
		?>
			Se ha subido la imagen.
			<form id="editImgForm"><input type="hidden" value='<?php print htmlentities($serItem); ?>' name="serItem"></form>
		<?php
		}
		else {
		?>
			Error desconocido.
		<?php
		}
	}
?>

<script type="text/javascript">
var categoryId=<?php print $my_item->category_id; ?>;
var itemId=<?php print $_POST["item_id"]; ?>;
	var catalogPointer=myCatalog;
	catalogPointer=catalogPointer.moveToChildren("categoryId", categoryId);
	if (!catalogPointer.myCategory.id) {
		catalogPointer=catalogPointer.moveToChildren("selected");
		catalogPointer=catalogPointer.moveToChildren("categoryId", categoryId);
	}

var serItem=document.getElementById("editImgForm").serItem.value;
for (var i in catalogPointer.items) {
	if (catalogPointer.items[i].id==itemId) {
		var myItem=catalogPointer.items[i];
		break;
	}
}
myItem.setFromArray(JSON.parse(serItem));
myItem.mySetView(catalogPointer.myItemView.cloneNode(true));

window.setTimeout(removeAlert,2000,document.getElementById('editImg'));
function removeAlert(alert) {
	alert.parentNode.removeChild(alert);
}
</script>
<?php
}
else {
?>
	<p id="alertMsg"></p>
	<form method="post" action="?section=edit_item_img" myFor="editImg" id="editImgForm" enctype="multipart/form-data">
	<input type="hidden" value="<?php print $_GET["item_id"]; ?>" name="item_id">
	<table class="mytable">
		<tr>
			<td colspan="2"><label>Archivo de imagen:<input name="Filedata" type="file"></label></td>
		</tr>
		<tr>
			<td><label><input type="radio" name="imageType" value="small" SIZE="20">Imagen pequeña (122px × 182px aprox) </label></td>
			<td><label><input type="radio" name="imageType" value="large" SIZE="20">Imagen grande (248px × 371px aprox) </label></td>
		</tr>
		<tr>
			<td><input type="submit" value="Enviar" name="submitButton"></td>
			<td><input type="button" value="Cerrar" myFor="editImg"></td>
		</tr>
	</table>
	</form>
<script type="text/javascript">
var myForm=document.getElementById("editImgForm");
myForm.anyRadioChecked=function() {
	var radios = this.getElementsByTagName('input');
	var radioChecked=false;
	for (var i = 0; i < radios.length; i++) {
	    if (radios[i].type === 'radio' && radios[i].checked) {
		radioChecked=true;
	    }
	}
	return radioChecked;
}
myForm.onsubmit=function () {
	var isChecked=this.anyRadioChecked();
	if (!isChecked) {
		alert("¡¡Seleccione por favor un tipo de tamaño para el archivo!!");
		return false;
	}
	else this.request();	
}
</script>
<?php
}
?>
</div>